#include "e:/code/game/gmooby2/include/gmooby.h"
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include <math.h>

float vx = 0.0f;
float vy = 0.0f;
float g = 9.8f;
float x = 0.0f;
float y = 0.0f;
float weakrad = 0.8f;

bool framefunc ()
{
  clearbuf ();
  if (keydown (GMK_K))
    screenfull (640, 480, 32);
  if (keydown (GMK_L))
    screenwin (640, 480);
  if (keydown (GMK_RIGHT))
    vx += 5.0f;
  if (keydown (GMK_LEFT))
    vx -= 5.0f;
  if (keydown (GMK_UP))
    vy -= 5.0f;
  if (keydown (GMK_DOWN))
    vy += 5.0f;
  glPushMatrix ();
  glTranslatef (x, y, 0);
  auxWireSphere (40);
  glPopMatrix ();
  x += vx * deltime ();
  y += vy * deltime ();
  vy += g;
  
  /* collision and energy weaken */
  if (x < 0.0f) {x = 0.0f; vx = fabs (vx); vx *= weakrad; }
  if (x > 640.0) {x = 640.0f; vx = -fabs (vx); vx *= weakrad; }
  if (y < 0.0f) {y = 0.0f; vy = fabs (vy); vy *= weakrad; }
  if (y > 480.0f) {y = 480.0f; vy = -fabs (vy); vy *= weakrad;}
  
  /* stop speed range */
  if (vx > -1 && vx < 1) vx = 0.0f;
  if (vy > -1 && vy < 1) {
    vy = 0.0f;
    if (fabs (y - 480.0f) < g)
      g = 0.0f;
  }
  glBegin (GL_LINES);
  glColor3f (1, 1, 1);
  glVertex2i (0, 0);
  glVertex2i (640, 480);
  glEnd ();
  
  swapbuf ();
  
  return false;
}

int gmain ()
{
  HWND hwnd = window (640, 480, "hilo");
  //screenwin (640, 480);
  opengfx (hwnd, 640, 480, 100);
  setbgc (0x00000000);
  setfps (30.0);
  gameloop (framefunc);
  closegfx ();
  return 0;
}
